package com.lzk.auth.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzk.auth.service.SysRoleService;
import com.lzk.auth.service.SysUserService;
import com.lzk.common.result.Result;
import com.lzk.common.utils.MD5;
import com.lzk.model.system.SysFactory;
import com.lzk.model.system.SysRole;
import com.lzk.model.system.SysUser;
import com.lzk.vo.system.SysUserQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Api(tags="用户管理接口")
@RestController
@RequestMapping("/admin/system/sysUser")
public class SysUserController {
    @Autowired
    private SysUserService service;
    @Autowired
    private SysRoleService sysRoleService;
    @ApiOperation("用户条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result index(@PathVariable Long page,
                        @PathVariable Long limit,
                        SysUserQueryVo sysUserQueryVo) {
        Page<SysUser> pageParam = new Page<>(page,limit);

        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();

        String username = sysUserQueryVo.getKeyword();

        if(!StringUtils.isEmpty(username)) {
            wrapper.like(SysUser::getUsername,username);
        }


        IPage<SysUser> pageModel = service.page(pageParam, wrapper);
        List<SysUser> records = pageModel.getRecords();

        for (SysUser record : records) {
            Long roleId = record.getRoleId();
            if(roleId!=null) {
                SysRole role = sysRoleService.getById(roleId);

                String roleName = role.getRoleName();
                record.setRoleName(roleName);
            }
        }



        return Result.ok(pageModel);
    }

    @ApiOperation(value = "获取用户")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        SysUser user = service.getById(id);
        return Result.ok(user);
    }

    @ApiOperation(value = "保存用户")
    @PostMapping("save")
    public Result save(@RequestBody SysUser user) {
        String password=user.getPassword();
        String passwordMD5 = MD5.encrypt(password);
        user.setPassword(passwordMD5);
        service.save(user);
        return Result.ok();
    }

    @ApiOperation(value = "更新用户")
    @PutMapping("update")
    public Result updateById(@RequestBody SysUser user) {
        service.updateById(user);
        return Result.ok();
    }

    @ApiOperation(value = "删除用户")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        UpdateWrapper<SysUser> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id); // 根据ID作为更新条件

        updateWrapper.set("is_deleted", 1);
        service.update(updateWrapper);

//        service.removeById(id);
        return Result.ok();
    }



}




